package com.oracle.dao;

import com.oracle.dao.impl.BookDao;
import com.oracle.mapper.BookMapper;
import com.oracle.pojo.Book;
import com.oracle.pojo.BookExample;
import com.oracle.pojo.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookDaoImpl implements BookDao {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public String addBook(Book record) {
        int result = bookMapper.insertSelective(record);
        if (result>0){
            return "YES";
        }
        return "NO";
    }

    @Override
    public List<Book> selectByExample(BookVo bookVo) {
        BookExample example = new BookExample();
        BookExample.Criteria criteria = example.createCriteria();
        if (bookVo.getName()!=null && !"".equals(bookVo.getName())){
            criteria.andTitleLike("%"+bookVo.getName()+"%");
        }
        if (bookVo.getCid()!=null && bookVo.getCid()!=0){
            criteria.andCidEqualTo(bookVo.getCid());
        }
        if (bookVo.getMinPrice()!=null){
            criteria.andBpriceGreaterThanOrEqualTo(bookVo.getMinPrice());
        }
        if (bookVo.getMaxPrice()!=null){
            criteria.andBpriceLessThanOrEqualTo(bookVo.getMaxPrice());
        }
        if (bookVo.getAuthor()!=null&&!bookVo.getAuthor().equals("")){
            criteria.andAuthorLike("%"+bookVo.getAuthor()+"%");
        }
        criteria.andStatedEqualTo(0);
        List<Book> list = bookMapper.selectNewBooksByExample(example);
        return list;
    }

    @Override
    public List<Book> selectByExample_All(BookVo bookVo) {
        BookExample example = new BookExample();
        BookExample.Criteria criteria = example.createCriteria();
        if (bookVo.getName()!=null && !"".equals(bookVo.getName())){
            criteria.andTitleLike("%"+bookVo.getName()+"%");
        }
        if (bookVo.getCid()!=null && bookVo.getCid()!=0){
            criteria.andCidEqualTo(bookVo.getCid());
        }
        if (bookVo.getMinPrice()!=null){
            criteria.andBpriceGreaterThanOrEqualTo(bookVo.getMinPrice());
        }
        if (bookVo.getMaxPrice()!=null){
            criteria.andBpriceLessThanOrEqualTo(bookVo.getMaxPrice());
        }
        if (bookVo.getAuthor()!=null&&!bookVo.getAuthor().equals("")){
            criteria.andAuthorLike("%"+bookVo.getAuthor()+"%");
        }
        List<Book> list = bookMapper.selectNewBooksByExample(example);
        return list;
    }

    //修改书本
    @Override
    public String updateByBid(Book book) {
        int code = bookMapper.updateByPrimaryKeySelective(book);
        if (code > 0){
            return "YES";
        }
        return "NO";
    }

    @Override
    public int down(Integer bid) {
        Book book = new Book();
        book.setBid(bid);
        book.setStated(1);
        int code = bookMapper.updateByPrimaryKeySelective(book);
        if (code>0){
            return 200;
        }
        return 400;
    }

    @Override
    public int up(Integer bid) {
        Book book = new Book();
        book.setBid(bid);
        book.setStated(0);
        int code = bookMapper.updateByPrimaryKeySelective(book);
        if (code>0){
            return 200;
        }
        return 400;
    }

    @Override
    public List<Book> selectBooksByNumOrder() {
        List<Book> list = bookMapper.selectBooksByNumOrder();
        if (!list.isEmpty()){
            return list;
        }
        return null;
    }

    @Override
    public Book seleteBookbyId(Book book) {
        BookExample example = new BookExample();
        BookExample.Criteria criteria = example.createCriteria();
        criteria.andBidEqualTo(book.getBid());
        List<Book> list = bookMapper.selectNewBooksByExample(example);
        Book bookVo = list.isEmpty()?null:list.get(0);
        return bookVo;
    }

    @Override
    public List<Book> selectBooksByCid(Integer cid) {
        return bookMapper.selectBooksByCid(cid);
    }
}
